package ar.uade.distribuidas.tpo.srv.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;







import javax.servlet.http.Part;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import ar.uade.distribuidas.tpo.base.dominiodto.RemitoCPEDTO;
import ar.uade.distribuidas.tpo.base.dominiodto.RemitoDTO;
import ar.uade.distribuidas.tpo.srv.dao.RemitoCPEXmlDAO;
import ar.uade.distribuidas.tpo.srv.dao.RemitoXmlDAO;
import ar.uade.distribuidas.tpo.srv.delegado.BD;

/**
 * Servlet implementation class RecibirMercaderia
 */
@WebServlet("/RecibirMercaderia")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10,      // 10MB
maxRequestSize=1024*1024*50)   // 50MB
public class RecibirMercaderia extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//private String archivo = "";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RecibirMercaderia() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("static-access")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		@SuppressWarnings("unused")
		RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/recibirmerca.jsp");
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Document doc;
		try {
			
		BD bd = BD.getInstance();
		//this.cuil = Long.valueOf(request.getParameter("cuil"));
		//ProveedorDTO pr = bd.getProveedor(cuil);
		Part parteArch = request.getPart("file");
		 InputStream filecontent = parteArch.getInputStream();
		 builder = factory.newDocumentBuilder();
			doc = builder.parse(filecontent);
			
		//String aux = getFilename(parteArch);
		//RemitoCPEXmlDAO oxml = RemitoCPEXmlDAO.getInstancia(filecontent);
		Set<RemitoCPEDTO> lop = RemitoCPEXmlDAO.getInstancia().recuperarRemitoCPEDTO(doc);
		List<RemitoDTO> remitos = bd.recibirMercaderia(lop);
		RemitoXmlDAO.getInstancia();
		String remitosXML ="";
		for(RemitoDTO rem : remitos){
			remitosXML = remitosXML + RemitoXmlDAO.getInstancia().remitoToXmlString(rem);
		}
		//dispatcher.forward(request, response);
		request.setAttribute("xmlgen1", remitosXML);

		request.setAttribute("allok", "Los remitos fueron procesada correctamente.");
		
		 request.getRequestDispatcher("/jsp/recibirmerca.jsp").forward(request, response);
		//dispatcher.forward(request, response);	
		} catch (Exception e) {
			//dispatcher = request.getRequestDispatcher("WEB-INF/jsp/allerror.jsp");
			request.setAttribute("allerror", "Hubo un error al procesar la solicitud.");
			request.getRequestDispatcher("/jsp/recibirmerca.jsp").forward(request, response);
			
			
			//dispatcher.forward(request, response);
		} 
		
	}
	
	/*private static String getFilename(Part part) {
	    for (String cd : part.getHeader("content-disposition").split(";")) {
	        if (cd.trim().startsWith("filename")) {
	            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
	            return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
	        }
	    }
	    return null;
	}*/

}
